package fpt.graph;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.StringTokenizer;
import fpt.myFileHandler.Handler;

/**
 * GraphFPT
 * 
 * r25
 * 
 * 12 March 2010
 * 
 * This class represents an undirected graph for the FPT Algorithm
 * Project. It takes an input of type Handler (from fpt.myFileHandler)
 * gets all relevant information needed to create a graph.
 * @author Daniel De Guzman
 *
 */
public class GraphFPT {
	private int k;
	private HashMap<String, String[]> graph;
	private List<String> vertexCover;
	private HashMap<String, String> removed;
	
	/**
	 * Constructor
	 * @param h - any type that extends Handler from fpt.myFileHandler
	 */
	public GraphFPT(Handler h){
		this.k = h.getK();
		this.graph = new HashMap<String, String[]>();
		this.vertexCover = new ArrayList<String>();
		this.initialize(h.getvInfoArray());
		this.removed = new HashMap<String, String>();
	}	
	
	/**
	 * Constructor for graphs after kernelization
	 * @param k - Parameter k
	 * @param graph - An undirected graph
	 * @param vertexCover - List of vertices included in the cover
	 * @param removed - HashMap of removed vertices as keys, and reasons of removal as values
	 */
	public GraphFPT(int k, HashMap<String, String[]> graph, List<String> vertexCover, HashMap<String, String> removed){
		this.k = k;
		this.graph = graph;
		this.vertexCover = vertexCover;
		this.removed = removed;
	}
	
	/**
	 * Creates graph structure using the Vertex Information array
	 * @param vInfoArray - Array containing the vertex information
	 */
	private void initialize(String[] vInfoArray){
		String vInfo = "";
		int size = vInfoArray.length;
		
		for (int i = 0; i < size; i++){
			vInfo = vInfoArray[i];
			StringTokenizer st = new StringTokenizer(vInfo);
			List<String> nTemp = new ArrayList<String>();
			String vertexLabel = "";
			
			while (st.hasMoreElements()){
				String label = st.nextToken();
				// Label is either a key or its neighbours
				if (label.endsWith(",")){
					label = label.substring(0, label.length()-1);
					vertexLabel = label;
				} else {
					nTemp.add(label);
				}
			}
			
			String[] neighbours = nTemp.toArray(new String[nTemp.size()]);
			graph.put(vertexLabel, neighbours);
		}
	}
	
	// Getters and Setters

	public int getK() {
		return k;
	}

	public void setK(int k) {
		this.k = k;
	}

	public HashMap<String, String[]> getGraph() {
		return graph;
	}

	public void setGraph(HashMap<String, String[]> graph) {
		this.graph = graph;
	}

	public List<String> getVertexCover() {
		return vertexCover;
	}

	public void setVertexCover(List<String> vertexCover) {
		this.vertexCover = vertexCover;
	}

	public void setRemoved(HashMap<String, String> removed) {
		this.removed = removed;
	}

	public HashMap<String, String> getRemoved() {
		return removed;
	}
	
	
}
